/*import Vue from 'vue'


let vm = new Vue()
vm.$on('dataChanged',()=>{
    console.log('datachanged')
})
vm.$on('dataChanged',()=>{
    console.log('datachagned1')
})
vm.$emit('dataChanged')*/


class EventEmmiter{
    constructor () {
        this.subs = Object.create(null)
    }

    $on (eventType, handler) {
      this.subs[eventType] = this.subs[eventType] || []
      this.subs[eventType].push(handler)  
    }

    $emit (eventType) {
        if(this.subs[eventType]){
            this.subs[eventType].forEach(handler =>{
                handler()
            })
        }
    }
}

class Dep{
    constructor(){
        this.subs = []
    }
    addSub(sub){
        if(sub !=null && sub.update){
            this.subs.push(sub)
            console.log('add update')
        }
        console.log(sub==null)

    }
    notify(){
        this.subs.forEach(sub =>{
            sub.update()
        })
    }

}

class Watcher{
    update(){
        console.log('update')
    }
}

let dep = new Dep();
let watcher = new Watcher()
dep.addSub(watcher);

dep.notify()

let em = new EventEmmiter()
em.$on('click',()=>{
    console.log('click1')
})

em.$on('click',()=>{
    console.log('click2')
})

em.$emit('click')